package com.letter.otools.day.user;

import android.os.Environment;
import android.view.View;

import androidx.appcompat.widget.Toolbar;

import com.blankj.utilcode.util.FileIOUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.JsonUtils;
import com.letter.otools.day.Anniversary;
import com.letter.otools.day.BaseActivity;
import com.letter.otools.day.R;
import com.letter.otools.day.view.KeyValueView;

import org.litepal.LitePal;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ToolsActivity extends BaseActivity implements View.OnClickListener {
    private Toolbar toolbar;
    private KeyValueView export;
    private KeyValueView importIn;
    List<Anniversary> anniversaryList = new ArrayList<>();

    @Override
    protected int getLayout() {
        return R.layout.activity_user_tools;
    }

    @Override
    protected void initView() {

    }

    @Override
    public void initBinder() {
        toolbar = findViewById(R.id.Toolbar);
        export = findViewById(R.id.Export);
        importIn = findViewById(R.id.importIn);
        export.setOnClickListener(this);
        importIn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.Export) {
            anniversaryList.clear();
            anniversaryList = LitePal.order("id asc").find(Anniversary.class);
            if (anniversaryList.isEmpty()) {
                File fileDoc = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
                File f = new File(fileDoc.getAbsolutePath(), "anniversary.dat");
                if (FileUtils.createOrExistsFile(f)) {
                    FileIOUtils.writeFileFromString(f, GsonUtils.toJson(anniversaryList));
                }
            }
        } else if (id == R.id.importIn) {
        }
    }
}
